import scala.collection.immutable.Iterable
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object Sheet3 {
  def main(args: Array[String]): Unit = {
    //    val a = Array(1,2,3,4,5)
    //
    //    val b = 0 until a.length
    //    b.foreach(print)
    //
    //    val c = b by -1
    //    c.foreach(print)
    //
    //    println()
    //
    //    val abuf = ArrayBuffer[Int]()
    //
    //    abuf.append(Array(1,2,3,4,5) :_*)
    //    abuf += (6,7,8,9,10)
    //
    //    val array = new Array[Int](10)
    //
    //    abuf.copyToArray(array)
    //
    //    println(array.mkString("[", ",", "]"))
    //
    //    import scala.collection.Searching._
    //    val searchResult = array.search(1)

    val result1 = randomNumbers(6)
    println(result1.mkString(", "))

    swapAdjacent1(result1)
    println(result1.mkString(", "))

    val result2 = swapAdjacent2(result1)
    println(result2.mkString(", "))

    val result3 = result2.filter(_ > 0)
    val result4 = result2.filter(_ <= 0)
    println(result3, result4)


    val array1 = Array(0.1, 0.2, 0.3, 0.4, 0.5)
    val result5 = array1.sum
    println(result5)

    val result6 = result1.sorted.reverse
    val result7 = result1.toBuffer.sorted.reverse
    println(result6.mkString(", "))
    println(result7.mkString(", "))


    val array2 = Array(1, 1, 2, 2, 3)
    val result8 = removeDuplicates1(array2)
    println(result8.mkString(", "))

    val array3 = Array(-1, -2, -3, 1, 2, 3)
    val result9 = keepFirstNegative1(array3)
    println("Keep first negative1: " + result9.mkString("[", ", ", "]"))

    val result10 = keepFirstNegative2(array3)
    println("Keep first negative2: " + result10.mkString("[", ", ", "]"))

    println("American timezone: " + americanTimezone().mkString(", "))

  }

  def randomNumbers(n: Int): Array[Int] = {
    val ret = new Array[Int](n)
    for (i <- ret.indices) {
      ret(i) = scala.util.Random.nextInt(n)
    }
    ret
  }

  def swapAdjacent1(array: Array[Int]): Unit = {
    for (i <- array.indices by 2 if (i + 1) < array.length) {
      val tmp = array(i)
      array(i) = array(i + 1)
      array(i + 1) = tmp
    }
  }

  def swapAdjacent2(array: Array[Int]): Iterable[Int] = {
    for (i <- array.indices by 2 if (i + 1) < array.length; j <- 1 to 0 by -1) yield array(i + j)
  }

  def removeDuplicates1(array: Array[Int]): Array[Int] = {
    val buffer = new ArrayBuffer[Int]

    for (elem <- array) {
      if (!buffer.contains(elem)) {
        buffer += elem
      }
    }
    buffer.toArray
  }

  def keepFirstNegative1(array: Array[Int]): Array[Int] = {
    val buffer = array.toBuffer

    val positionsToRemove = for (i <- array.indices if array(i) < 0) yield i
    for (i <- positionsToRemove.drop(1).reverse) {
      buffer.remove(i)
    }
    buffer.toArray
  }

  def keepFirstNegative2(array: Array[Int]): Array[Int] = {
    val buffer = array.toBuffer

    var alreadyHaveNegative = false
    val positionsToKeep =
      for {
        i <- array.indices
        if (!alreadyHaveNegative && array(i) < 0) || array(i) >= 0
      } yield {
        if (array(i) < 0) {
          alreadyHaveNegative = true
        }
        i
      }

    for (i <- positionsToKeep.indices) {
      buffer(i) = array(positionsToKeep(i))
    }

    buffer.trimEnd(array.length - positionsToKeep.length)

    buffer.toArray
  }

  def americanTimezone(): Array[String] = {
    java.util.TimeZone.getAvailableIDs.filter(_.startsWith("America")).map(_.drop(8)).sorted
  }

  def removeDuplicates2(array: Array[Int]): Array[Int] = {
    array.distinct
  }

  def flavor(): mutable.Buffer[String] = {
    import java.awt.datatransfer._

    import scala.collection.JavaConverters._
    val flavors = SystemFlavorMap.getDefaultFlavorMap.asInstanceOf[SystemFlavorMap]
    flavors.getNativesForFlavor(DataFlavor.imageFlavor).asScala
  }
}
